<template>
  <div class="xtx-dialog" v-show="visiable" :class="{ fade }">
    <div class="wrapper" :class="{ fade }">
      <div class="header">
        <h3>{{ title }}</h3>
        <a @click="closeDialog" href="Javascript:;" class="iconfont icon-close-new"></a>
      </div>
      <!-- 对话框主体 -->
      <div class="body">
        <slot></slot>
      </div>
      <div class="footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
export default {
  name: 'XtxDialog',
  props: {
    title: {
      type: String,
      default: ''
    },
    visiable: {
      type: Boolean,
      default: false
    }
  },
  setup(props, { emit }) {
    const fade = ref(null)
    // 监控父组件传来的visiable控制对话框的显示和隐藏  控制fade的值就可以
    watch(
      () => props.visiable,
      (newVal) => {
        setTimeout(() => {
          fade.value = newVal
        }, 0)
      },
      { immediate: true }
    )

    // 点击关闭按钮关闭对话框，向父组件传递事件
    const closeDialog = () => {
      emit('update:visiable', false)
    }
    return { fade, closeDialog }
  }
}
</script>

<style lang="less" scoped>
.xtx-dialog {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8887;
  background: rgba(0, 0, 0, 0);
  &.fade {
    transition: all 0.5s;
    background: rgba(0, 0, 0, 0.5);
  }
  .wrapper {
    width: 600px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -60%);
    opacity: 0;
    &.fade {
      transition: all 0.5s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }
  }
  .body {
    padding: 20px 40px;
    font-size: 16px;
    max-height: 600px;
    overflow: auto;
    .icon-warning {
      color: @priceColor;
      margin-right: 3px;
      font-size: 16px;
    }
  }
  .footer {
    text-align: center;
    padding: 10px 0 30px 0;
  }
  .header {
    position: relative;
    height: 70px;
    line-height: 70px;
    padding: 0 20px;
    border-bottom: 1px solid #f5f5f5;
    h3 {
      font-weight: normal;
      font-size: 18px;
    }
    a {
      position: absolute;
      right: 25px;
      top: 25px;
      font-size: 24px;
      width: 20px;
      height: 20px;
      line-height: 20px;
      text-align: center;
      color: #999;
      &:hover {
        color: #666;
      }
    }
  }
}
</style>
